//
//  SEGNewSelectCommunityCell.m
//  SEGUhomeStandard
//
//  Created by 揭婧雯 on 2018/8/14.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGNewSelectCommunityCell.h"

@interface SEGNewSelectCommunityCell ()
/**
 小区名称
 */
@property (nonatomic, strong)UILabel *communityLabel;
/**
 城市名称
 */
@property (nonatomic, strong)UILabel *cityLabel;
/**
 删除按钮
 */
@property (nonatomic, strong)UIButton *deleteButton;
/**
 展开按钮
 */
@property (nonatomic, strong)SEGAdjustableButton *showAllButton;
/** 展开按钮文字 */
@property (nonatomic, strong) UILabel *showAllLab;
/** 展开按钮图片 */
@property (nonatomic, strong) UIImageView *showAllImg;
/**
 重新定位按钮
 */
@property (nonatomic, strong)SEGAdjustableButton *locateButton;
/**
 当前cell的indexPath
 */
@property (nonatomic, strong)NSIndexPath *indexPath;
/**
 分割线
 */
@property (nonatomic, strong) UIView *line;

@end

@implementation SEGNewSelectCommunityCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.backgroundColor = kColor_W;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    //        self.frame = CGRectMake(0, 0, ScreenWidth, 191);
    [self setupUI];
    //    }
    return self;
}

-(void)setupUI{
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    UILabel *label = [[UILabel alloc] init];
    label.font = kFont_16;
    label.textColor = kColor_1;
    [self.contentView addSubview:label];
    //    [label mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.contentView).offset(15);
    //        make.centerY.equalTo(self.contentView);
    //    }];
    self.cityLabel = label;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.font = kFont_16;
    label2.textColor = kColor_1;
    [self.contentView addSubview:label2];
    //    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.cityLabel.mas_right).offset(15);
    //        make.centerY.equalTo(self.contentView);
    //    }];
    self.communityLabel = label2;
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    self.line = line;
}


/**
 * 展示cell样式
 * @author Wendy
 * @param type  cell类型
 * @param cityName  城市名称
 * @param communityName  小区名称
 * @param indexPath  cell所在位置
 */
- (void)showCellWithType:(SEGNewSelectCommunityCellType)type
                cityName:(NSString *)cityName
           communityName:(NSString *)communityName
               indexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
    self.cityLabel.text = ValueToString(cityName);
    self.communityLabel.text = ValueToString(communityName);
    self.deleteButton.hidden = YES;
    self.showAllButton.hidden = YES;
    self.locateButton.hidden = YES;
    CGFloat communityLabelMarginRight = 40.0;
    // 计算城市宽度 & 其宽度不超过屏幕
    CGFloat cityLabelWidth = MIN(ceil([ValueToString(cityName) widthForFont:kFont_16]) + 10, LYScreenWidth() - 60);
    
    //根据不同类型调整UI布局
    switch (type) {
        case SEGNewSelectCommunityCellType_Search:
        {
            // 字体大小有变 重新计算宽度
            cityLabelWidth = MIN(ceil([ValueToString(cityName) widthForFont:kFont_13]) + 10, LYScreenWidth() - 60);
            self.communityLabel.hidden = NO;
            self.cityLabel.hidden = NO;
            self.line.hidden = NO;
            [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(15);
                make.centerY.equalTo(self.contentView);
                make.right.equalTo(self.cityLabel.mas_left).offset(-5);
            }];
    
            [self.cityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-15);
                make.centerY.equalTo(self.contentView);
                make.width.mas_equalTo(cityLabelWidth);
            }];
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentView).mas_offset(-0.5);
                make.left.right.equalTo(self.contentView);
                make.height.mas_equalTo(0.5);
            }];
            self.cityLabel.textColor = kColor_3;
            self.cityLabel.font = kFont_13;
            self.cityLabel.textAlignment = NSTextAlignmentRight;
            break;
        }
        case SEGNewSelectCommunityCellType_City:
        {
            self.communityLabel.hidden = YES;
            self.cityLabel.hidden = NO;
            self.line.hidden = YES;
            [self.cityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(15);
                make.width.mas_equalTo(cityLabelWidth);
                make.centerY.equalTo(self.contentView);
            }];
            self.cityLabel.textColor = kColor_1;
            self.cityLabel.font = kFont_16;
            self.cityLabel.textAlignment = NSTextAlignmentLeft;
            break;
        }
        case SEGNewSelectCommunityCellType_Community:
        {
            self.communityLabel.hidden = NO;
            self.cityLabel.hidden = YES;
            self.line.hidden = NO;
            [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(30);
                make.right.mas_equalTo(-communityLabelMarginRight);
                make.centerY.equalTo(self.contentView);
            }];
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentView).mas_offset(-0.5);
                make.left.right.equalTo(self.contentView);
                make.height.mas_equalTo(0.5);
            }];
            break;
        }
        case SEGNewSelectCommunityCellType_History:
        {
            self.deleteButton.hidden = NO;
            self.communityLabel.hidden = NO;
            self.line.hidden = NO;
            if (cityName && cityName.length > 0) {//防止城市为空
                self.cityLabel.hidden = NO;
                [self.cityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView).offset(30);
                    make.centerY.equalTo(self.contentView);
                    make.width.mas_equalTo(cityLabelWidth);
                }];
                [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.cityLabel.mas_right).offset(5);
                    make.right.mas_equalTo(-communityLabelMarginRight);
                    make.centerY.equalTo(self.contentView);
                }];
                
                self.cityLabel.textColor = kColor_1;
                self.cityLabel.font = kFont_16;
                self.cityLabel.textAlignment = NSTextAlignmentLeft;
            } else {
                self.cityLabel.hidden = YES;
                [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView).offset(30);
                    make.right.mas_equalTo(-communityLabelMarginRight);
                    make.centerY.equalTo(self.contentView);
                }];
            }
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentView).mas_offset(-0.5);
                make.left.equalTo(self.contentView).mas_offset(30);
                make.right.equalTo(self.contentView).mas_offset(-15);
                make.height.mas_equalTo(0.5);
            }];
            break;
        }
        case SEGNewSelectCommunityCellType_ShowAll:
        {
            self.communityLabel.hidden = YES;
            self.cityLabel.hidden = YES;
            self.line.hidden = NO;
            self.showAllButton.hidden = NO;
            self.showAllButton.selected = self.isShowAll;
            if (self.showAllButton.selected) {
                self.showAllLab.text = @"收起";
                self.showAllImg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_packup")];
            } else {
                self.showAllLab.text = @"展开全部记录";
                self.showAllImg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_default")];
            }
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentView).mas_offset(-0.5);
                make.left.right.equalTo(self.contentView);
                make.height.mas_equalTo(0.5);
            }];
            break;
        }
        case SEGNewSelectCommunityCellType_CurrentCity:
        {
            self.communityLabel.hidden = YES;
            self.cityLabel.hidden = NO;
            self.locateButton.hidden = NO;
            self.line.hidden = YES;
            [self.cityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(15);
                make.centerY.equalTo(self.contentView);
                make.right.equalTo(self.locateButton.mas_left).offset(-15);
            }];
            if(cityName && cityName.length > 0) {
                self.cityLabel.text = [NSString stringWithFormat:@"当前定位城市：%@", cityName];
            }else {
                self.cityLabel.text = @"系统未定位到城市，请手动选择";
            }
            break;
        }
    }
}

- (void)seg_deleteButtonClicked
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(deleteButtonClickedWithIndexPath:)]) {
        [self.delegate deleteButtonClickedWithIndexPath:self.indexPath];
    }
}

- (void)seg_showAllButtonClicked {
    if (self.delegate && [self.delegate respondsToSelector:@selector(showAllButtonClicked)]) {
        [self.delegate showAllButtonClicked];
    }
}

- (void)seg_locateButtonClicked
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(locateButtonClicked)]) {
        [self.delegate locateButtonClicked];
    }
}

- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        UIImage *deleteImage = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_close")];
        _deleteButton = [[UIButton alloc] init];
        [_deleteButton setImage:deleteImage forState:UIControlStateNormal];
        [self.contentView addSubview:_deleteButton];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.width.mas_equalTo(deleteImage.size.width + 30);
            make.height.equalTo(self.contentView);
        }];
        [_deleteButton addTarget:self action:@selector(seg_deleteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (SEGAdjustableButton *)showAllButton {
    if (!_showAllButton) {
        _showAllButton = [[SEGAdjustableButton alloc] init];
        [_showAllButton addTarget:self
                           action:@selector(seg_showAllButtonClicked)
                 forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_showAllButton];
        [_showAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.right.mas_equalTo(0);
        }];
        _showAllLab = [[UILabel alloc] init];
        _showAllLab.font = kFont_13;
        _showAllLab.textColor = kColor_1;
        _showAllLab.text = @"展开全部记录";
        //【汇v1.0.0】iOS 选择社区，历史记录模块，展开全部记录点击无响应
        _showAllLab.userInteractionEnabled = NO;
        [_showAllButton addSubview:_showAllLab];
        [_showAllLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.top.bottom.mas_equalTo(0);
        }];
        _showAllImg = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_default")];
        _showAllImg.image = image;
        _showAllImg.userInteractionEnabled = YES;
        [_showAllButton addSubview:_showAllImg];
        [_showAllImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_showAllButton.mas_centerY);
            make.left.equalTo(_showAllLab.mas_right).mas_offset(5);
            make.height.mas_equalTo(image.size.height);
            make.width.mas_equalTo(image.size.width);
        }];
    }
    return _showAllButton;
}

- (SEGAdjustableButton *)locateButton {
    if (!_locateButton) {
        _locateButton = [[SEGAdjustableButton alloc] init];
        [_locateButton setTitleColor:kColor_T forState:UIControlStateNormal];
        _locateButton.titleLabel.font = kFont_16;
        
        [_locateButton setTitle:@"重新定位" forState:UIControlStateNormal];
        [self.contentView addSubview:_locateButton];
        [_locateButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);//因为右边有索引时占位15，contentView宽度变小，直接用cell
            make.centerY.equalTo(self.contentView);
            make.width.mas_equalTo(70);
        }];
        [_locateButton addTarget:self action:@selector(seg_locateButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _locateButton;
}

@end
